<?php

namespace app\api\service;

use think\facade\Db;

class IntegralOrderService
{
    /**
     * 保存订单信息
     *
     * */
    public static function saveOrder($user_id,$param,$goods,$redeem_integral,$real_amount,$order_no)
    {


        Db::startTrans();
        try {
            $order_id =  Db::name('order')->insertGetId([
                'order_type' => 3,
                'user_id' => $user_id,
                'store_id' => $param['store_id'],
                'order_no' =>  $order_no,
                'payment_type' => 4,
                'real_amount' => $real_amount,
                'redeem_integral' => $redeem_integral,
                'receiver_name' => $param['receiver_name'],
                'receiver_phone' => $param['receiver_phone'],
                'province' => $param['province'],
                'city' => $param['city'],
                'county' => $param['county'],
                'address' => $param['address'],
                'remark' => !empty($data['remark']) ? $data['remark'] : '',
                'status' => 1,
                'create_time' => time(),
            ]);

            Db::name('order_item')->insert([
                'order_id' => $order_id,
                'goods_id' => $goods['goods_id'],
                'goods_name' => $goods['goods_name'],
                'attr' =>  !empty($goods['attr']) ? $goods['attr'] : '',
                'image' => $goods['image'],
                'market_price' => $goods['sell_price'],
                'sell_price' => $goods['redeem_price'],
                'buy_num' => $param['buy_num'],
                'redeem_integral' => $redeem_integral,
                'total_amount' => $real_amount
            ]);

            // 提交事务
            Db::commit();

            return $order_id;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return null;
        }

    }




    /**
     * 获取单规格积分商品信息
     * */
    public static function findSingleIntegralGoods($goods_id)
    {
        $goods =  Db::name('goods')
                    ->alias('a')
                    ->field('b.goods_id,a.goods_name,a.spec_type,a.main_image,a.sell_price,b.redeem_stock,b.redeem_price,b.redeem_integral')
                    ->join('integral_goods b','a.id=b.goods_id')
                    ->where(['b.id' => $goods_id,'b.status' => 1])
                    ->find();

        return  $goods;
    }

    /**
     * 获取多规格商品信息
     * */
    public static function findManyIntegralGoods($sku_id)
    {
        $goods = Db::name('goods_item')
                    ->field('a.goods_id,a.attr,a.image,a.sell_price,b.goods_name,b.main_image,b.spec_type,c.redeem_price,c.redeem_integral,c.redeem_stock')
                    ->alias('a')
                    ->join('goods b','a.goods_id=b.id')
                    ->join('integral_goods_item c','a.id=c.sku_id')
                    ->join('integral_goods d','d.id=c.integral_goods_id')
                    ->where(['c.id' => $sku_id,'d.status' => 1])
                    ->find();

        return  $goods;
    }



}